package org.dtrd.modules.faq.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.common.system.vo.LoginUser;
import org.dtrd.modules.content.articles.entity.bean.FaqInfo;
import org.dtrd.modules.content.articles.entity.vo.DtrdExtEmsYwFaqsVO;
import org.dtrd.modules.faq.entity.dto.FaqDetail;
import org.dtrd.modules.faq.entity.dto.FaqListInfo;
import org.dtrd.modules.faq.entity.po.DtrdExtEmsYwFaqs;
import org.dtrd.modules.faq.entity.request.FaqListRequest;
import org.dtrd.modules.faq.mapper.DtrdExtEmsYwFaqsMapper;
import org.dtrd.modules.faq.service.IDtrdExtEmsYwFaqsService;
import org.dtrd.modules.util.ShiroUtil;
import org.dtrd.common.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 知识问答 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-06
 */
@Service
@DS("multi-datasource1")
public class DtrdExtEmsYwFaqsServiceImpl extends ServiceImpl<DtrdExtEmsYwFaqsMapper, DtrdExtEmsYwFaqs> implements IDtrdExtEmsYwFaqsService {

    @Override
    public List<FaqDetail> getFaqList(Integer requestId) {
        LambdaQueryWrapper<DtrdExtEmsYwFaqs> queryWrapper = baseQueryWrapper();
        List<DtrdExtEmsYwFaqs> poList = list(queryWrapper);
        List<FaqDetail> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream()
                    .map(po -> new FaqDetail().parseFromPo(po))
                    .collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public IPage<DtrdExtEmsYwFaqsVO> getFaqList(Integer pageNum, Integer pageSize, Integer requestId) {
        LambdaQueryWrapper<DtrdExtEmsYwFaqs> queryWrapper = baseQueryWrapper();
        IPage<DtrdExtEmsYwFaqs> page = new Page<>(pageNum, pageSize);
        page = page(page, queryWrapper);
        return page.convert(po -> {
            DtrdExtEmsYwFaqsVO dtrdExtEmsYwFaqsVO = new DtrdExtEmsYwFaqsVO(po);
            return dtrdExtEmsYwFaqsVO;
        });
    }

    @Override
    public FaqDetail getFaq(Integer dataId) {
        DtrdExtEmsYwFaqs dtrdExtEmsYwFaqs = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwFaqs)
                .map(po -> new FaqDetail().parseFromPo(po))
                .orElse(null);
    }

    @Override
    public boolean saveOrUpdateFaq(FaqDetail dto) {
        LoginUser loginUser = ShiroUtil.getLoginUser();
        DtrdExtEmsYwFaqs dtrdExtEmsYwFaqs = new DtrdExtEmsYwFaqs();
        if (dto.getDataId() == null) {
            dtrdExtEmsYwFaqs = dtrdExtEmsYwFaqs.init();
            if (loginUser != null) {
                dtrdExtEmsYwFaqs.setFaqCreater(loginUser.getId());
            }
        } else {
            if (loginUser != null) {
                dtrdExtEmsYwFaqs.setFaqEditor(loginUser.getId());
            }
            dtrdExtEmsYwFaqs.setFaqEditTime(new Date());
        }
        dtrdExtEmsYwFaqs.parseFromDto(dto);
        return saveOrUpdate(dtrdExtEmsYwFaqs);
    }

    @Override
    public boolean removeFaq(Integer dataId) {
        DtrdExtEmsYwFaqs query = this.getById(dataId);
        if (query == null) {
            return false;
        }
        LoginUser loginUser = ShiroUtil.getLoginUser();
        String deleter = null;
        if (loginUser != null) {
            deleter = loginUser.getId();
        }
        return baseMapper.deleteFaq(dataId, deleter, new Date());
    }

    @Override
    public IPage<FaqListInfo> pageFaq(FaqListRequest request) {
        Integer faqCategoryId = request.getFaqCategoryId();
        String faqQuestion = request.getFaqQuestion();
        LambdaQueryWrapper<DtrdExtEmsYwFaqs> queryWrapper = baseQueryWrapper();
        if (faqCategoryId != null) {
            queryWrapper.eq(DtrdExtEmsYwFaqs::getFaqCategoryid, faqCategoryId);
        }
        if (StrUtil.isNotBlank(faqQuestion)) {
            queryWrapper.like(DtrdExtEmsYwFaqs::getFaqQuestion, StringUtil.fuzzySearchWrapper(faqQuestion));
        }
        IPage<DtrdExtEmsYwFaqs> queryPage = new Page<>(request.getPage(), request.getPageSize());
        IPage<DtrdExtEmsYwFaqs> poList = this.page(queryPage, queryWrapper);
        return poList.convert(FaqListInfo::new);
    }

    @Override
    public List<FaqInfo> getLatestRecords(int limitSize) {
        LambdaQueryWrapper<DtrdExtEmsYwFaqs> queryWrapper = baseQueryWrapper().orderByDesc(DtrdExtEmsYwFaqs::getFaqViews)
                .orderByDesc(DtrdExtEmsYwFaqs::getDataId);
        if (limitSize > 0) {
            queryWrapper.last(" limit " + limitSize);
        }
        List<DtrdExtEmsYwFaqs> list = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return list.stream().map(FaqInfo::new).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public FaqInfo getFagInfoById(Integer dataId) {
        DtrdExtEmsYwFaqs dtrdExtEmsYwFaqs = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwFaqs)
                .map(po -> new FaqInfo().parseFromPo(po))
                .orElse(null);
    }

    public LambdaQueryWrapper<DtrdExtEmsYwFaqs> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdExtEmsYwFaqs.class)
                .eq(DtrdExtEmsYwFaqs::getIsDel, 0);
    }

    public LambdaUpdateWrapper<DtrdExtEmsYwFaqs> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdExtEmsYwFaqs.class)
                .eq(DtrdExtEmsYwFaqs::getIsDel, 0);
    }
}
